from OpenGL.GL import *

class Lighting(object):
	"""
	This class represents the lighting of the scene.
	"""
	
	def __init__(self):
		"""
		Constructor.
		"""
		
		# Dictionary that contains all the lights of the scene. 
		self.lights = {}
	
	def addLight(self, id):
		"""
		Adds a light in the scene. id should be GL_LIGHTn.
		"""
		
		newLight = Light(id)
		self.lights[id] = newLight
		
	def setLight(self, id, position=None, diffuse=None, specular=None, ambient=None):
		"""
		Sets the id light. id should be GL_LIGHTn.
		"""
		
		self.lights[id].set(position, diffuse, specular, ambient)
		
	def render(self, id):
		"""
		Renders a light, given its id.
		"""
		
		self.lights[id].render()

class Light(object):
	"""
	This class represents an OpenGL light.
	"""
	
	def __init__(self, id):
		"""
		Constructor.
		"""
		
		self.id = id
		glEnable(id)
		
		self.position = [0, 0, 0, 1]
		self.diffuse = [0, 0, 0, 1]
		self.specular = [0, 0, 0, 1]
		self.ambient = [0, 0, 0, 1]
		
	def set(self, position=None, diffuse=None, specular=None, ambient=None):
		"""
		Sets the light attributes.
		"""
		
		if not(position is None):
			self.position = position
		if not(diffuse is None):
			self.diffuse = diffuse
		if not(specular is None):
			self.specular = specular
		if not(ambient is None):
			self.ambient = ambient
		
		self.commit()
		
	def commit(self):
		"""
		Commit current light attributes to the OpenGL core.
		"""
				
		glLight(self.id, GL_POSITION, self.position[:3])
		glLight(self.id, GL_DIFFUSE, self.diffuse)
		glLight(self.id, GL_SPECULAR, self.specular)
		glLight(self.id, GL_AMBIENT, self.ambient)
		
	def render(self):
		"""
		Renders the light.
		"""
		
		glLight(self.id, GL_POSITION, self.position[:3])